// pages/issue/issue.js
var inputContent = {}
var now=new Date()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputContent: {},
    money:'',
    gId:0,
    pId:0,
    category:'',
    payersNum:0,
    tip:'',
    date:now,
    addUrl: "http://127.0.0.1:8080/superadmin/addaccount",
    list: [],
    selectilall: true,
    show:false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData:['餐饮','零食','水果','交通','生活用品','水电','其他'],//下拉列表的数据
    index:0//选择的下拉列表下标
    },
    bindChange: function(e) {
    inputContent[e.currentTarget.id] = e.detail.value
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
      that.setData({
        gId:options.gId,
        pId:options.uId,
      })
      var s = 'http://localhost:8080/superadmin/getgroupfriendlistbyid?gId='+that.data.gId;
      console.log(s);
      wx.request({
        url:  'http://localhost:8080/superadmin/getgroupfriendlistbyid?gId='+that.data.gId,
        method:'GET',
        data:{},
        success:function(res){
          var datalist=res.data.groupfriendlist;
          if(datalist==null){
            var toastText = '获取数据失败' + res.data.errMsg;
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            });
            console.log("列表获取失败");
          }else{
            for(var i = 0; i < datalist.length; i++) {
              datalist[i].checked = true;
              that.data.payersNum++;
              if(datalist[i].uId==that.data.pId)
              {
                datalist.splice(i,1);
                i--;
              }
            }
            that.setData({
              list:datalist
            })
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //单选
  select: function (e) {
    let selectValue = e.currentTarget.dataset.name
    let index = e.currentTarget.dataset.index;
    let list = this.data.list
    let newli = 'list[' + index + '].checked';
    // this.setData({
    //   [newli]: !this.data.list[index].checked
    // })
    let num = 0;
    for(var i=0;i<this.data.list.length;i++){
      if(this.data.list[i].checked){
        num++;
      }
    }
    if(num == this.data.list.length){
      this.setData({
        selectilall: true
      })
    }else{
      this.setData({
        selectilall: true
      })
    }
  },
  //全选，取消全选
  selectAll: function (e) {
    let list = this.data.list;
    let selectilall = this.data.selectilall;
    if (selectilall == false) {
      for (let i = 0; i < list.length; i++) {
        let newli = 'list[' + i + '].checked';
        this.setData({
          [newli]: true,
          selectilall: true
        })
      }
    } else {
      for (let i = 0; i < list.length; i++) {
        let newli = 'list[' + i + '].checked';
        this.setData({
          [newli]: true,
          selectilall: true
        })
      }
    }
  },

  // 点击下拉显示框
 selectTap(){
  this.setData({
   show: !this.data.show
  });
  },
  // 点击下拉列表
  optionTap(e){
  let Index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
  this.setData({
   index:Index,
   show:!this.data.show
  });
  },

  formSubmit: function (e) {
    var that = this;
    var url = that.data.addUrl;
    var index = that.data.index;
    var flag=false;
    var option=false;
    var len=that.data.list.length;
    for (let i = 0; i < len; i++) {
      if(that.data.list[i].checked==true){
          option=true;
          break;
      }
    }
    if(option==false){
      flag=true;
    }
    that.setData({
      money:e.detail.value.money,
      tip:e.detail.value.tip,
      category:that.data.selectData[index],
     });
    
     if (that.data.money == "") {
      var toastText = "金额不能为空";
      wx.showToast({
        title: toastText,
        icon: 'none',
        duration: 2000
      });
    } else if(that.data.money=="0"){
      var toastText = "金额不能为零";
      wx.showToast({
        title: toastText,
        icon: 'none',
        duration: 2000
      });
    }else if(flag==true){
      var toastText = "请选择指定人";
      wx.showToast({
        title: toastText,
        icon: 'none',
        duration: 2000
      });
    }
    else{
    wx.request({
      url: url,
      data: {"gId":that.data.gId,"money":that.data.money,"category":that.data.category,"tip":that.data.tip,"pId":that.data.pId,"payersNum":that.data.payersNum,"payerList":null,"date":that.data.date,"state":0},
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var result = res.data.success;
        var toastText = "操作成功！";
        if (result != true) {
          toastText = "操作失败" + res.data.errMsg;
        } ;
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 500
        });
        setTimeout(function () { 
          var app = getApp();
          // 拿到全局数据，并改变它
          app.globalData++;
          wx.navigateBack({
            delta: 1,
          })  
       }, 500)
      }
    })
  }
  }
})